<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class akun extends CI_Controller {
	
	function __construct(){		
		parent::__construct();	
		//$this->load->library('Excel_generator');
		//$this->output->enable_profiler(true);
		
		$this->load->model('mdl_akun');
	}
	
	function index(){
		$data['page_title']='Daftar Akun';
		$data['page_icon']='icon-home';
		$data['page_content']='akun_list';
		
		$this->load->view('layout/master',$data);
	}
	
	
	function grid(){
		echo json_encode($this->mdl_akun->get_data());
	}
	
	function add(){
		$data['aksi'] = 'add';

		$data['kode_akun'] = '';
        $data['parent'] = '';
        $data['tipe'] = 'H';
        $data['deskripsi'] = '';
        $data['status'] = '1';

        $data['kode_akun_1'] = '';
        $data['kode_akun_2'] = '';
		
		$this->load->view('akun_form', $data);
	}

	function edit($kode_akun){
		$data['aksi'] = 'edit';

		$res = $this->mdl_akun->get_data_edit($kode_akun);

		$data['kode_akun'] = $kode_akun;
        $data['parent'] = $res->row()->parent;
        $data['tipe'] = $res->row()->tipe;
        $data['deskripsi'] = $res->row()->deskripsi;
        $data['status'] = '1';

        $ka = explode('-', $kode_akun);

        $data['kode_akun_1'] = $ka[0];
        $data['kode_akun_2'] = $ka[1];
		
		$this->load->view('akun_form', $data);
	}
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		$data['kode_akun'] = $data['kode_akun_1'].'-'.$data['kode_akun_2'];

		# rules validasi form
		$this->form_validation->set_rules("parent", 'Kode Akun', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kode_akun_1", 'Kode Akun', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kode_akun_2", 'Kode Akun', 'trim|required|xss_clean');
		$this->form_validation->set_rules("deskripsi", 'Deskripsi', 'trim|required|xss_clean');
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			$data["pesan_error"] .= trim(form_error('parent',' ',' '))==''?'':form_error('parent',' ',' ').'<br>';
			$data["pesan_error"] .= trim(form_error('kode_akun_1',' ',' '))==''?'':form_error('kode_akun_1',' ',' ').'<br>';
			$data["pesan_error"] .= trim(form_error('kode_akun_2',' ',' '))==''?'':form_error('kode_akun_2',' ',' ').'<br>';
			$data["pesan_error"] .= trim(form_error('deskripsi',' ',' '))==''?'':form_error('deskripsi',' ',' ').'<br>';

		}else{
			if($aksi == 'add'){
				$result = !$this->mdl_akun->kode_exist($data['kode_akun']);
				if($result){
					$result = $this->mdl_akun->InsertOnDb($data);
				}else{
					$data['pesan_error'] = 'Sudah terdapat kode akun.';
				}
			}else{ // edit
				// jika ada perubahan kode
				if($data['kode'] != $data['kode_akun']){
					$result = !$this->mdl_akun->kode_exist($data['kode_akun']);
					if($result){
						$result = $this->mdl_akun->UpdateOnDb($data);
					}else{
						$data['pesan_error'] = 'Sudah terdapat kode akun.';
					}
				}else{
					$result = $this->mdl_akun->UpdateOnDb($data);
				}
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
		
	}
}